all files / lib/ page.js

98.15% Statements 53/54
93.1% Branches 27/29
100% Functions 8/8
100% Lines 48/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                              78×                         13×                                                     38× 38× 38× 38×   38× 38×   38×         38×                                                 12×                     10× 10× 10× 10× 10×              
'use strict';
 
var inherit = require('./utils').inherit;
var Facade = require('./facade');
var Track = require('./track');
var isEmail = require('is-email');
 
/**
 * Initialize new `Page` facade with `dictionary`.
 *
 * @param {Object} dictionary
 *   @param {string} category
 *   @param {string} name
 *   @param {Object} traits
 *   @param {Object} options
 * @param {Object} opts
 *   @property {Boolean|Undefined} clone
 */
 
function Page(dictionary, opts) {
  Facade.call(this, dictionary, opts);
}
 
/**
 * Inherit from `Facade`
 */
 
inherit(Page, Facade);
 
/**
 * Get the facade's action.
 *
 * @return {string}
 */
Page.prototype.action = function() {
  return 'page';
};
 
Page.prototype.type = Page.prototype.action;
 
/**
 * Fields
 */
 
Page.prototype.category = Facade.field('category');
Page.prototype.name = Facade.field('name');
 
/**
 * Proxies.
 */
 
Page.prototype.title = Facade.proxy('properties.title');
Page.prototype.path = Facade.proxy('properties.path');
Page.prototype.url = Facade.proxy('properties.url');
 
/**
 * Referrer.
 */
Page.prototype.referrer = function() {
  return this.proxy('context.referrer.url')
    || this.proxy('context.page.referrer')
    || this.proxy('properties.referrer');
};
 
/**
 * Get the page properties mixing `category` and `name`.
 *
 * @param {Object} aliases
 * @return {Object}
 */
Page.prototype.properties = function(aliases) {
  var props = this.field('properties') || {};
  var category = this.category();
  var name = this.name();
  aliases = aliases || {};
 
  if (category) props.category = category;
  if (name) props.name = name;
 
  for (var alias in aliases) {
    var value = this[alias] == null
      ? this.proxy('properties.' + alias)
      : this[alias]();
    Iif (value == null) continue;
    props[aliases[alias]] = value;
    Eif (alias !== aliases[alias]) delete props[alias];
  }
 
  return props;
};
 
/**
 * Get the user's email, falling back to their user ID if it's a valid email.
 *
 * @return {string}
 */
Page.prototype.email = function() {
  var email = this.proxy('context.traits.email') || this.proxy('properties.email');
  if (email) return email;
 
  var userId = this.userId();
  if (isEmail(userId)) return userId;
};
 
/**
 * Get the page fullName.
 *
 * @return {string}
 */
Page.prototype.fullName = function() {
  var category = this.category();
  var name = this.name();
  return name && category
    ? category + ' ' + name
    : name;
};
 
/**
 * Get event with `name`.
 *
 * @return {string}
 */
Page.prototype.event = function(name) {
  return name
    ? 'Viewed ' + name + ' Page'
    : 'Loaded a Page';
};
 
/**
 * Convert this Page to a Track facade with `name`.
 *
 * @param {string} name
 * @return {Track}
 */
Page.prototype.track = function(name) {
  var json = this.json();
  json.event = this.event(name);
  json.timestamp = this.timestamp();
  json.properties = this.properties();
  return new Track(json, this.opts);
};
 
/**
 * Exports.
 */
 
module.exports = Page;